import axios from 'axios'
import { useRegular } from './rules'

// https://www.1905.com/search/index-p-type-all-q-
const videoUrl = '/videoUrl/index-p-type-all-q-'
const originUrl = 'https://www.1905.com/mdb/film'
const videoDetail = '/videoDetail'
let startTime = 0
let endTime = 0

/**
 * 获取视频信息
 * @param {string} name 文件名称
 * @returns
 */
async function getVideoInfo(name) {
    if (startTime == 0) {
        startTime = new Date()
    }
    const url = videoUrl + name + '.html'
    const html = getHtml()
    const str = await html(url)
    const cover = batch(str, [/<div[^>]*class="movie-pic"[^>]*>.*?<\/div>/gm, /src="https.*.jpg/gm], 'src="')
    const other = await clickTitle(str)
    endTime = new Date()
    return {
        cover,
        ...other,
        addDate: new Date(),
    }
}

function getVideoTitle(str) {
    let res = batch(str, [/<h1>.*?<span>/gm])
    res = res.replaceAll('<span>', '')
    res = res.replaceAll('<h1>', '')
    return res
}

function getScore(str) {
    return batch(str, [/<spanclass="score">.*<\/span>/gm, /[0-9]<\/b>.[0-9]/gm], '</b>')
}

// 进入二级页面
async function clickTitle(str) {
    try {
        const url = batch(str, [/my_video_h2_old.+?target/gm, /https.*\//gm], originUrl)
        const html = getHtml()
        const str1 = await html(videoDetail + url)
        // console.log(str1)
        const score = getScore(str1) ?? 0
        const category = getCategory(str1)
        const newName = getVideoTitle(str1)
        return { score, category, newName, url: originUrl + url }
    } catch (e) {
        console.log(e)
        return {}
    }
}

function getCategory(str) {
    let _str = str
    _str = useRegular(_str, /mdbypsy_jsy_lx.*?<\/span>/gm, false)
    if (_str.length > 0) {
        _str = useRegular(_str[0], /[\u4e00-\u9fa5]+/gm, false)
        return _str
    }
    return []
}

/**
 *
 * @param {String} str 要处理的字符串
 * @param {Array} reList 正则表达式
 * @param {String} replace 用来替换的字符串
 * @returns
 */
function batch(str = '', reList = [], replace = '') {
    let _str = str
    for (const re of reList) {
        _str = useRegular(_str, re, false)
        if (_str?.length > 0) {
            _str = _str[0]
        }
    }
    try {
        return _str?.replace(replace, '')
    } catch (e) {
        console.log(_str)
        return ''
    }
}

/**
 * 使用闭包减少请求次数
 * @param {String} url 要匹配的url
 * @param {Boolean} blank 是否去掉空格
 * @param {Boolean} wrap 是否去掉换行
 * @returns {Function}
 */
function getHtml() {
    let res = null
    let _url = ''
    /**
     * @param {String} url 要匹配的url
     * @param {Boolean} blank 是否去掉空格
     * @param {Boolean} wrap 是否去掉换行
     */
    return async (url, blank = true, wrap = true) => {
        let html = ''
        if (_url !== url) {
            res = await axios.get(url)
            res = res.data
            _url = url
        }
        if (wrap) {
            html = res.replace(/<\/?.+?>/g, '')
            html = res.replace(/[\r\n]/g, '')
        }
        if (blank) html = res.replace(/\s+/g, '')
        return html
    }
}

function getTime() {
    console.log(`getVideoInfo use ${endTime - startTime}s`)
    startTime = 0
    endTime = 0
}

export { getVideoInfo, getTime }
